<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreBespeak;
use App\Http\Requests\UpdateBespeak;
use App\Models\Bespeak;
use App\Models\BespeakCate;
use App\Models\Sjorder;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BespeakController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        view()->share([
            '_yyx' => 'am-in',
            '_bespeak' => 'am-active',
        ]);

    }


    //私教规格管理
    public function index()
    {
        //所有门店
        $cates=Store::all();
        $bescates=Bespeak::orderBy('sort')->paginate(10);

        return view('admin.bespeak.index',compact('bescates','cates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //私教规格新增页面
    public function create()
    {
     //全部私教


        return view('admin.bespeak.create');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    //执行新增
    public function store(StoreBespeak $request)
    {
        $info=$request->all();
        $info['total_price']=$request->one_price * $request->course_number;
      Bespeak::create($info);
      return redirect('admin/bespeak')->with('success','新增项目成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //加载编辑页面
    public function edit($id)
    {


        //当前规格
        $bespeak=Bespeak::find($id);

        return view('admin.bespeak.edit',compact('bespeak'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //执行更新
    public function update(UpdateBespeak $request, $id)
    {
        //当前私教规格
        $bespeak=Bespeak::find($id);
        //规格名验证唯一性
        $info=Bespeak::where('name',$request->name)->first();
        if($info && $bespeak->name != $request->name){
            return back()->with('success','所修改规格名已有其他规格使用');
        }
        $info=$request->all();
        $info['total_price']=$request->one_price * $request->course_number;
       $bespeak->update($info);
        return redirect('admin/bespeak')->with('success','修改项目成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //删除
    public function destroy($id)
    {
        if($id==15){
           return back()->with('success','请不要删除单次这一条') ;
        }
        $info=Sjorder::where('bespeak_id',$id)->where('is_pay',1)->first();
        if($info){
            return back()->with('success','该课程有会员在上课，请不要删除') ;
        }else{

            Bespeak::destroy($id);
            return back()->with('success','删除成功');
        }

    }

    //ajax排序
    public function sort(Request $request){
        $bespeak = Bespeak::find($request->id);
        $bespeak->sort = $request->sort;
        $bespeak->save();
    }

    //ajax改变使用状态
    public function change_attr(Request $request)
    {

        $bespeak = Bespeak::find($request->id);

        if ($bespeak->is_use == 0) {
            $data['is_use'] = 1;
            $bespeak->update($data);
        } else {
            $data['is_use'] = 0;
            $bespeak->update($data);
        }
    }

}
